﻿using DbSync;
using DBSync.Util;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DBSync.wpfPage
{
    /// <summary>
    /// Interaction logic for LoginSettingsPage.xaml
    /// </summary>
    public partial class Login : Page
    {
        syncdbEntities syncdb;
        public static Login instance;
        public Login()
        {
            InitializeComponent();
            syncdb = new syncdbEntities();
            resultLabel.Content = "";
        }
        public static Login Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new Login();
                }

                return instance;
            }
        }

        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            string username = txtUsername.Text;
            string password = txtPassword.Password;
            List<tbl_user> userList = new List<tbl_user>();
            resultLabel.Content = "";

            if (!username.Equals("") && !password.Equals(""))
            {

                try
                {
                    userList = syncdb.tbl_user.Where(u => u.USERNAME.Equals(username) && u.PASSWORD.Equals(password) && u.STATUS == 0).ToList();
                    if (userList.Count > 0)
                    {
                        SessionData.user = userList[0];
                        if (SessionData.user.ROLE.Trim() == "admin")
                        {
                            MainWindow.Instance.ContentFrame.Content = OrganizationPage.Instance;
                        }
                        else if (SessionData.user.ROLE.Trim() == "user")
                        {
                            tbl_organization organization = syncdb.tbl_organization.Where(o => o.ID == SessionData.user.ORGANIZATION_ID).FirstOrDefault();
                            
                            if (organization==null)
                            {
                                MSGWindow msgw = new MSGWindow("Error", "Your organization details not found. Please contact administrator");
                                msgw.Show();
                            }
                            else
                            {
                                SessionData.organization = organization;
                                if (organization != null)
                                {
                                    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                                    var settings = config.AppSettings.Settings;
                                    bool InitialSet = Convert.ToBoolean(settings[SyncKeys.INITIALSET].Value);

                                    if (!InitialSet)
                                    {
                                        SyncKeys.PHRDBNAME = SyncKeys.PHRDBNAME_DEFAULT + "_" + SessionData.organization.ID;
                                        SyncKeys.DCDRDBNAME = SyncKeys.DCDRDBNAME_DEFAULT + "_" + SessionData.organization.ID;

                                        SyncConnection connection = SyncConnection.Instance;
                                        SyncConnection.updateConnectionStrings();
                                    }
                                }                                                                    
                                MainWindow.Instance.ContentFrame.Content = HomePage.Instance;
                            }
                        }
                    }
                    else if (username == "axiohelix" && password == "syncAdmin")
                    {
                        tbl_organization org = new tbl_organization();
                        org.ORGANIZATION_NAME = "axiohelix";
                        tbl_user user = new tbl_user();
                        user.USERNAME = username;
                        SessionData.organization = org;
                        SessionData.user = user;
                        MainWindow.Instance.ContentFrame.Content = HomePage.Instance;
                    }
                    else
                    {
                        resultLabel.Content = "Invalid username or password";
                    }
                }
                catch (Exception ex)
                {
                    MSGWindow msgw = new MSGWindow("Error", "Database connection failure. Check the Internet connection");
                    msgw.Show();          
                } 
                
            }

        }

        private void btnSettingsReset_Click(object sender, RoutedEventArgs e)
        {
            resultLabel.Content = "";
            txtPassword.Password = "";
            txtUsername.Text = "";
        }

        private void Page_Loaded_1(object sender, RoutedEventArgs e)
        {
            MainWindow.Instance.btn_settings.Visibility = System.Windows.Visibility.Hidden;
        }

    }
}
